// pages/classHomePage/pk/doexam/doexam.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activity_id: null,
    item: null,
    studentPrefix: null,
    student_answer_list: [],
    exampaper: null,
    exampaper_list: [],
    check_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this
    setTimeout(() => {
      if (options != null) {
        console.log('页面跳转传值：', options)
        _this.setData({
          activity_id: options['activity_id'],
          item: options['item'],
        })
        _this.student_get_exam(options['item'])
      }
    }, 1000)
  },

  student_get_exam: function (item) {
    let _this = this
    let data = {}
    app.formPost('/api/admin/student/exampaper/select/' + String(item), data).then(res => {
      res = JSON.parse(JSON.stringify(res['response']).replace(/<[^>]*>/g, ''))
      console.log('试卷exampaper_list:', res['titleItems'][0]['questionItems']);
      _this.setData({
        exampaper: res,
        check_list: new Array(res['titleItems'][0]['questionItems'].length).fill(0),
        exampaper_list: res['titleItems'][0]['questionItems']
      })
      // console.log("res['titleItems'][0]['questionItems']['items'].length",res['titleItems'][0]['questionItems'][0]['items'].length);
      for (let index = 0; index < res['titleItems'][0]['questionItems'].length; index++) {
        _this.data.check_list[index] = new Array(res['titleItems'][0]['questionItems'][index]['items'].length).fill(0)
      }
    })
  },

  student_answer: function (e) {
    let _this = this
    console.log('答案：', e.target.dataset);
    e.target.dataset = e.currentTarget.dataset
    let prefixindex = e.target.dataset['prefixindex']
    let newCheckList = _this.data.check_list.slice()
    // console.log("newCheckList[e.target.dataset['num'][prefixIndex]]",newCheckList[e.target.dataset['num']][prefixindex]);
    if (newCheckList[e.target.dataset['num']][prefixindex] == 0) {
      newCheckList[e.target.dataset['num']][prefixindex] = 1
    } else if (newCheckList[e.target.dataset['num']][prefixindex] == 1) {
      newCheckList[e.target.dataset['num']][prefixindex] = 0
    }
    _this.setData({
      check_list: newCheckList
    })
    console.log('_this.data.check_list:', _this.data.check_list);

    if (_this.data.check_list[e.target.dataset['num']][prefixindex] == 1) {
      let item = e.target.dataset['item']
      let studentprefix = e.target.dataset['studentprefix']
      let data = {
        [item]: studentprefix
      }
      // console.log(data);
      _this.data.student_answer_list.push(data)
    } else if (_this.data.check_list[e.target.dataset['num']][prefixindex] == 0) {
      let item = e.target.dataset['item']
      let studentprefix = e.target.dataset['studentprefix']
      let data = {
        [item]: studentprefix
      }
      // console.log(data);
      _this.data.student_answer_list.pop(data)
    }
    console.log('_this.data.student_answer_list:', _this.data.student_answer_list);
  },


  submit: function () {
    let _this = this
    let data = {
      'account': wx.getStorageSync('account'),
      'people': '学生',
      'id': _this.data.item,
      'doTime': 12
    }
    for (let index = 0; index < _this.data.student_answer_list.length; index++) {
      data = {
        ...data,
        ..._this.data.student_answer_list[index]
      }
    }
    console.log('准备提交答案data:', data);
    app.formPost('/api/admin/student/exampaper/answer/answerSubmit?', data).then(res => {
      console.log('提交答案结果：', res);
      wx.navigateBack({
        delta: 1
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})